﻿using MX.Core.ORM;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VC.Models;
using Dapper;

namespace VC.BLL
{
    public class VideoBLL
    {
        public static bool Insert(Video video)
        {
            using (var conn = DatabaseHelper.Get())
            {
                return conn.Execute(@"INSERT INTO  `video` ( 
                                                  `MemberId`,
                                                  `Title`,
                                                  `VideoUrl`,
                                                  `CoverUrl`,
                                                  `Width`,
                                                  `Height`,
                                                  `Duration`,
                                                  `Size`,
                                                  `HotValue`,
                                                  `Attr`,
                                                  `Enable`,
                                                  `Audit`,
                                                  `CreateTime`,
                                                  `Hits`
                                                ) 
                                                VALUES
                                                  ( 
                                                    @MemberId,
                                                    @Title,
                                                    @VideoUrl,
                                                    @CoverUrl,
                                                    @Width,
                                                    @Height,
                                                    @Duration,
                                                    @Size,
                                                    @HotValue,
                                                    @Attr,
                                                    @Enable,
                                                    @Audit,
                                                    @CreateTime,
                                                    @Hits
                                                  )", video) > 0;
            }
        }

        public static PagerResult<Video> List(string keywords, int pageSize, int pageIndex)
        {
            using (var conn = DatabaseHelper.Get())
            {
                string sql = $"select * from video where Enable=1 {(string.IsNullOrEmpty(keywords) ? "" : "and title like @Keywords")} order by createTime desc";
                return conn.PageList<Video>(sql,
                    pageIndex, pageSize, new { Keywords = $"%{keywords}%" });
            }
        }


        public static bool SetStatus(long videoId, bool enable)
        {
            using (var conn = DatabaseHelper.Get())
            {
                return conn.Execute("update video set Enable=@Enable where VideoId=@VideoId", new { Enable = enable, VideoId = videoId }) > 0;
            }
        }


        public static PagerResult<Video> ApiList(int pageSize, int pageIndex)
        {
            using (var conn = DatabaseHelper.Get())
            {
                string sql = $"select * from video where Enable=1 and Audit=1 order by Attr desc,HotValue desc,Hits desc,createTime desc";
                return conn.PageList<Video>(sql,
                    pageIndex, pageSize);
            }
        }
    }
}
